package com.whity.towerdefense.model;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.whity.towerdefense.view.GraphicEltFactory;

import android.util.Log;

public class TowerType
{
	// =============================================================
	// Private attributs :
	
	private static final String		TAG = "com.djm.test03.model";
	
	// General attributs
	private String 					m_Name;
	private String					m_GraphicHeartPath;
	
	// Statistic of the tower
	private float					m_Life;
	private float					m_Armor;
	private float					m_HpRegen;
	private float					m_Size;		// in Cell, diameter of the circle defining this tower.
	
	private ArrayList<ShootType>	m_ShootTypes;
	/*
	private float					m_Range;
	private float					m_Damage;
	private float					m_FireRate;	// in shot per second
	private float					m_SplashRadius;
	private float					m_SplashReduction;
	private int						m_Bounce;
	private float					m_BounceReduction;
	*/
	// =============================================================
	// Private functions
	
	private TowerType(String p_Name, String p_GraphicHeartPath, float p_Life, float p_Armor, float p_HpRegen, float p_Size, ArrayList<ShootType> p_ShootTypes)
	{
		m_Name				= p_Name;
		m_GraphicHeartPath 	= p_GraphicHeartPath;
		m_Life 				= p_Life;
		m_Armor 			= p_Armor;
		m_HpRegen			= p_HpRegen;
		m_Size				= p_Size;
		m_ShootTypes 		= p_ShootTypes;
	}
	
	// =============================================================
	// Public functions
	
	public TowerType(XmlPullParser p_XmlParser)
	{
		m_ShootTypes 		= new ArrayList<ShootType>();
		m_Name				= "";
		m_GraphicHeartPath 	= "";
		m_Life 				= -1;
		m_Armor 			= -1;
		m_HpRegen			= -1;
		m_Size				= -1;
		
		try
		{
	        int eventType = p_XmlParser.getEventType();
	        while(eventType != XmlPullParser.END_DOCUMENT)
	        {
	        	
	        	if(eventType == XmlPullParser.START_TAG)
	            {
	        		if(p_XmlParser.getName().equals("TowerType"))
	            	{
	        			m_Name 				= p_XmlParser.getAttributeValue(null, "Name");
	        			m_GraphicHeartPath	= p_XmlParser.getAttributeValue(null, "Graphic");
	        			GraphicEltFactory.CreateGraphicElt(m_GraphicHeartPath);
	        			m_Life 				= Float.parseFloat(p_XmlParser.getAttributeValue(null, "Life"));
	        			m_Armor 			= Float.parseFloat(p_XmlParser.getAttributeValue(null, "Armor"));
	        			m_Size				= Float.parseFloat(p_XmlParser.getAttributeValue(null, "Size"));
	            	}
	        		else if(p_XmlParser.getName().equals("ShotType"))
	        		{
	        			ShootType shootType = new ShootType(p_XmlParser);
	        			m_ShootTypes.add(shootType);
	        		}
	            }
	        	eventType = p_XmlParser.next();
	        }
		}
		catch (IOException e)
	    {
	    	Log.w(TAG, e.toString());
		}
		catch (XmlPullParserException e)
		{
			Log.w(TAG, e.toString());
		}
	}
	
	public Tower CreateTower(int p_X, int p_Y)
	{
		return new Tower(m_Name, m_Life, m_Armor, m_HpRegen, m_Size, GraphicEltFactory.GetGraphicHeart(m_GraphicHeartPath), m_ShootTypes, p_X, p_Y);
	}
	
	public TowerType Clone()
	{
		return new TowerType(m_Name, m_GraphicHeartPath, m_Life, m_Armor, m_HpRegen, m_Size, m_ShootTypes);
	}
	
	public String GetName()
	{
		return m_Name;
	}
	
	public String GetGraphicHeartPath()
	{
		return m_GraphicHeartPath;
	}
	
	// =============================================================
}
